﻿#include "menubar.h"
#include "ui_menubar.h"


MenuBar::MenuBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MenuBar)
{
    ui->setupUi(this);
    initMenu();
    initConnections();
    disableDisplayMenu();
}

void MenuBar::initConnections(){
    //发出改变主题的信号
    connect(themeChoiceGroup, &QActionGroup::triggered, [this](QAction* action){
        emit changeTheme(themeChoiceGroup->actions().indexOf(action));
    });

    //打开文件
    connect(openFile, &QAction::triggered, this, &MenuBar::openFileDialog);

    //新建文件
    connect(newFile, &QAction::triggered, this, &MenuBar::newFileDialog);

    //关闭文件
    connect(closeFile, &QAction::triggered, this, &MenuBar::closeCurFile);

    //发出改变显示类型的信号
    connect(displayTypeGroup, &QActionGroup::triggered, [this](QAction* action){
        emit changeDisplayType(dispalyMenu->actions().indexOf(action));
    });

    //退出选项按下
    connect(exit, &QAction::triggered,[this]{
        window()->close();
    });

    //关键字查询按下
    connect(search, &QAction::triggered, this, &MenuBar::showSearch);

    //统计信息
    connect(stats, &QAction::triggered, this, &MenuBar::showState);
}

//初始化菜单栏
void MenuBar::initMenu(){
    QAction* tmp = nullptr;

    //文件菜单的初始化
    fileMenu = new QMenu(" 文件 ",this);//尚待增加选项
    openFile = new QAction("打开家谱文件", this);
    newFile = new QAction("新建家谱文件", this);
    closeFile = new QAction("关闭家谱文件", this);
    exit = new QAction("退出", this);
    fileMenu->addAction(openFile);
    fileMenu->addAction(newFile);
    fileMenu->addAction(closeFile);
    fileMenu->addAction(exit);

    //查看菜单的初始化
    dispalyMenu = new QMenu(" 查看 ", this);
    displayTypeGroup = new QActionGroup(this);
    QList<QString> typeNames;
    typeNames.append("树状图");
    typeNames.append("列形图");
    for(QString name:typeNames){
        tmp = new QAction(name, this);
        tmp->setCheckable(true);
        displayTypeGroup->addAction(tmp);
        dispalyMenu->addAction(tmp);
    }

    dispalyMenu->actions().front()->setChecked(true);
    dispalyMenu->addSeparator();

    search = new QAction("信息搜索", this);
    stats = new QAction("统计数据", this);
    dispalyMenu->addAction(search);
    dispalyMenu->addAction(stats);

    //主题菜单的初始化
    themeMenu = new QMenu(" 主题 ",this);
    themeChoiceGroup = new QActionGroup(this);
    themeNames.append("靛青");
    themeNames.append("玉红");
    themeNames.append("铜绿");
    themeNames.append("橘橙");
    themeNames.append("钢蓝");
    themeNames.append("魏紫");
    themeNames.append("姜黄");
    themeNames.append("瓦灰");
    themeNames.append("桃粉");
    themeNames.append("茶白");
    for(QString name:themeNames){
        tmp = new QAction(name, this);
        tmp->setCheckable(true);
        themeChoiceGroup->addAction(tmp);
        themeMenu->addAction(tmp);
    }
    themeMenu->actions().front()->setChecked(true);

    //添加菜单进菜单栏
    menuBar = new QMenuBar(this);
    menuBar->addMenu(fileMenu);
    menuBar->addMenu(dispalyMenu);
    menuBar->addMenu(themeMenu);


    //把菜单栏加入控件中，采用垂直布局
    QVBoxLayout* vBox = new QVBoxLayout(this);
    vBox->addWidget(menuBar);
    vBox->setSpacing(0);
    vBox->setContentsMargins(0, 0, 0, 0);
    this->setLayout(vBox);
}

MenuBar::~MenuBar()
{
    delete ui;
}

//在没有文件打开时，关闭查看的菜单选项
void MenuBar::disableDisplayMenu(){
    search->setDisabled(true);
    stats->setDisabled(true);
    for(QAction* tp:displayTypeGroup->actions()){
        tp->setDisabled(true);
    }
}

//打开查看的菜单选项
void MenuBar::enableDisplayMenu(){
    search->setEnabled(true);
    stats->setEnabled(true);
    for(QAction* tp:displayTypeGroup->actions()){
        tp->setEnabled(true);
    }
    dispalyMenu->actions().front()->setChecked(true);
}

//主题颜色的更新，主要是对样式表中颜色的参数进行修改
void MenuBar::updateTheme(QColor newTheme, QColor oldTheme){
    QString stylesheet = styleSheet();
    stylesheet.replace(oldTheme.name().remove("#"),newTheme.name().remove("#"));
    setStyleSheet(stylesheet);
}

//改变主题菜单的选项，主要是软件初始化时调用
void MenuBar::setThemeIndex(int index){
    themeChoiceGroup->actions()[index]->setChecked(true);
}
